package com.example.lotterysystem.common.result;

import com.example.lotterysystem.common.errorcode.ErrorCode;
import com.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

//Serializable 用来序列化
@Data
public class CommonResult<T> implements Serializable {
    /**
     * 返回的数据码
     */
    private Integer code;
    /**
     * 正常返回数据
     */
    private T data;
    /**
     * 错误码描述
     */
    private String msg;

    public static <T> CommonResult<T> success(T data){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        commonResult.setData(data);
        commonResult.setMsg("");
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"code 没有发送异常");//判断为false就会返回message
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public static <T> CommonResult<T> error(ErrorCode code){
        return error(code.getCode(),code.getMsg());
    }

}
